

*insheet using ""

* From data in matlab
rename var1 market
rename var2 market_id
rename var3 firm

duplicates drop
sort market firm

* Generate this by saving the shares from the base and counterfactuals
use "FILEPATH/MatlabCodeServer_Dslurm/cf_v8_11122016/cf_v8_out_11122016.dta"


* From cf outcomes
rename var4 share0
rename var5 share_sim

rename var6 share_cf1
rename var7 share_cf2
rename var8 share_cf3
rename var9 share_cf4
rename var10 share_cf5

rename var11 share_cf6
rename var12 share_cf7
rename var13 share_cf8
rename var14 share_cf9
rename var15 share_cf10
rename var16 share_cf11

rename var17 share_cf12
rename var18 share_cf13

rename var19 lease_to_market

bysort firm: gen firm_count = _n
bysort firm: egen share0_mean = mean(share0)
bysort firm: egen share_sim_mean = mean(share_sim)

bysort market: gen market_count = _n

local cf = 1
local maxcf = 13

forval i = 1(1)`maxcf' {
capture confirm var share_cf`i'
if !_rc {
bysort firm: egen share_cf`i'_mean = mean(share_cf`i')
}
}
sort firm
br if firm_count == 1

bysort market: egen hhi0 = sum(share0^2)
bysort market: egen hhi_sim = sum(share_sim^2)
forval i = 1(1)`maxcf' {
capture confirm var share_cf`i'
if !_rc {
bysort market: egen hhi_cf`i' = sum(share_cf`i'^2)
}
}
sort market
br if market_count == 1


gen firmCount0 = share0*lease_to_market
gen firmCount_sim = share_sim*lease_to_market
forval i = 1(1)`maxcf' {
capture confirm var share_cf`i'
if !_rc {
gen firmCount_cf`i' = share_cf`i'*lease_to_market
}
}

bysort firm: egen firmCount0_sum = sum(firmCount0)
bysort firm: egen firmCount_sim_sum = sum(firmCount_sim)
forval i = 1(1)`maxcf' {
capture confirm var share_cf`i'
if !_rc {
bysort firm: egen firmCount_cf`i'_sum = sum(firmCount_cf`i')
}
}
sort firm
br if firm_count == 1

* Calculate shares using the count of leases actually signed by firms

bysort market: egen lease_to_market0 = sum(firmCount0)
bysort market: egen lease_to_market_sim = sum(firmCount_sim)
forval i = 1(1)`maxcf' {
capture confirm var share_cf`i'
if !_rc {
bysort market: egen lease_to_market_cf`i' = sum(firmCount_cf`i')
}
}

gen share_est0 = firmCount0/lease_to_market0
gen share_est_sim = firmCount_sim/lease_to_market_sim
forval i = 1(1)`maxcf' {
capture confirm var share_cf`i'
if !_rc {
gen share_est_cf`i' = firmCount_cf`i'/lease_to_market_cf`i'
}
}

bysort market: egen hhi_est0 = sum(share_est0^2)
bysort market: egen hhi_est_est = sum(share_est_sim^2)
forval i = 1(1)`maxcf' {
capture confirm var share_cf`i'
if !_rc {
bysort market: egen hhi_est_cf`i' = sum(share_est_cf`i'^2)
}
}
sort market
br if market_count == 1

bysort firm: egen share_dev = sd(share0)
bysort firm: egen share_sim_dev = sd(share_sim)

gen counter = 1
bysort firm: egen share_N = sum(counter)
bysort firm: egen share_sim_N = sum(counter)
drop counter


gen upper0 = share0_mean + 1.96*share_dev/sqrt(share_N)
gen lower0 = share0_mean - 1.96*share_dev/sqrt(share_N)

gen upper_sim = share_sim_mean + 1.96*share_sim_dev/sqrt(share_sim_N)
gen lower_sim = share_sim_mean - 1.96*share_sim_dev/sqrt(share_sim_N)


graph twoway (line share_sim_mean firm, color(gray) lpattern(dash)) ///
    (line share0_mean  firm, color(black)) , ///
	legend(nobox  order(2 "Observed" 1 "Simulated") region(lstyle(none))) ///
	xtitle("Firms") ytitle("Market Share") ///
	title("Market Share - Mean") ///
	saving(meanshare, replace) 

graph twoway (rarea upper_sim lower_sim firm, color(ebg)) ///
    (rline upper0 lower0 firm, color(black)) , ///
	legend(nobox  order(2 "Observed" 1 "Simulated") region(lstyle(none))) ///
	xtitle("Firms") ytitle("Market Share") ///
	title("Market Share - 95% Confidence Intervals") ///
	saving(confidenceshare, replace) 

gr combine meanshare.gph confidenceshare.gph, ysize(8) col(1) iscale(.7273) graphregion(margin(zero)) saving(marketshare_v18_b2, replace)
graph2tex, epsfile(marketshare)

gen meanHHI = .
gen stdHHI = .
gen cfobs = _n
bysort market_id: gen market_ct = _n


egen temp = mean(hhi_est0) if market_ct == 1
sort market_id temp
by market_id: carryforward temp,replace
replace meanHHI = temp if cfobs == 1
drop temp
egen temp = sd(hhi_est0) if market_ct == 1
sort market_id temp
by market_id: carryforward temp,replace
replace stdHHI = temp if cfobs == 1
drop temp

egen temp = mean(hhi_est_est) if market_ct == 1
sort market_id temp
by market_id: carryforward temp,replace
replace meanHHI = temp if cfobs == 2
drop temp
egen temp = sd(hhi_est_est) if market_ct == 1
sort market_id temp
by market_id: carryforward temp,replace
replace stdHHI = temp if cfobs == 2
drop temp

local maxcf = 13
forval i = 1(1)`maxcf' {
egen temp = mean(hhi_est_cf`i') if market_ct == 1
sort market_id temp
by market_id: carryforward temp,replace
replace meanHHI = temp if cfobs == `i' + 2
drop temp
egen temp = sd(hhi_est_cf`i') if market_ct == 1
sort market_id temp
by market_id: carryforward temp,replace
replace stdHHI = temp if cfobs == `i' + 2
drop temp
}
sum hhi_est_est if market_ct == 1
gen lowerHHI = meanHHI - 1.96*abs(stdHHI)/sqrt(r(N))
sum hhi_est_est if market_ct == 1
gen upperHHI = meanHHI + 1.96*abs(stdHHI)/sqrt(r(N))
sort cfobs

local bundletype = "cf"
local cflabel = `"1 "Observed" 2 "Baseline" 3 "Alpha_0"	4 "Royalty_0.25" 5 "Uniform" 6 "No_Landmen" 7 "No_Capacity" 8 "Any_Clause" 9 "Env_Clause" 10 "Noise_Clause" 11 "Subsurface_Clause" 12 "Surface_Damage_Clause" "'
sum meanHHI if cfobs == 2
local meandash = r(mean)

graph twoway (rarea upperHHI lowerHHI cfobs if cfobs < 13, color(ebg)) ///
    (connected meanHHI cfobs if cfobs  < 13, color(black) msize(tiny)) ///
	, legend(off) graphregion(color(white)) bgcolor(white) ylabel(,nogrid)  ///
	xtitle("Counterfactuals") ytitle("HHI") xlabel(``bundletype'label', angle(45)) ///
	yline(`meandash', lcolor(black) lpatter(dash)) saving(confidenceshare`cf', replace) 

********************************************************************************
* Welfare hist
********************************************************************************

clear
local v = 1
local b = 2
local inc = 1
insheet using `"FILEPATH/v`v'ival`b'inc`inc'.csv"', comma
local k = 1
foreach var in parcel firm  parcelout quality {
forval i = 1(1)10 {
rename v`k' `var'`i'
local k = `k' + 1
}
}

sum firm1 firm2 firm5
sum parcel1 parcel2 parcel5
sum parcel1, det
local bd = r(p95)
twoway (hist firm1 if firm1 > 0, color(midblue) bin(50)) (hist firm2 if firm2 > 0, color(ltblue) bin(50)) (hist firm5 if firm5 > 0, color(edkblue) bin(50)) ///
	(hist parcel1 if parcel1 > 0 & parcel1 < `bd', color(midgreen) bin(50)) (hist parcel2 if parcel2 > 0, color(eltgreen) bin(50)) (hist parcel5 if parcel5 > 0, color(dkgreen) bin(50)) ///
	, legend(order(1 "Parcel Base" 2 "Parcel +1" 3 "Parcel +5" 4 "Firm Base" 5 "Firm +1" 6 "Firm +5") cols(3) size(small))	///
	graphregion(color(white)) bgcolor(white) ylabel(,nogrid) ylabel(,labsize(vsmall)) xlabel(, labsize(vsmall)) ///
	name(hold, replace) ytitle(, size(small)) xtitle(, size(small))
graph save hold, replace
graph export utility`v'ival`b'.png, replace
	
twoway (hist quality1 if quality1 > 0, color(midblue) bin(50)) (hist quality2 if quality2 > 0, color(ltblue) bin(50)) (hist quality5 if quality5 > 0, color(edkblue) bin(50)) ///
	, legend(order(1 "Quality Base" 2 "Quality +1" 3 "Quality +5") cols(3) size(small))	///
	graphregion(color(white)) bgcolor(white) ylabel(,nogrid) ylabel(,labsize(vsmall)) xlabel(, labsize(vsmall)) ///
	name(hold, replace) ytitle(, size(small)) xtitle(, size(small))
graph save hold, replace
graph export quality`v'ival`b'.png, replace
	
clear
insheet using `"FILEPATH/land_v`v'ival`b'inc`inc'.csv"', comma
local k = 1
foreach var in frac {
forval i = 1(1)10 {
rename v`k' `var'`i'
local k = `k' + 1
}
}

twoway (hist frac1 if frac1 > 0, color(midblue) bin(50)) (hist frac2 if frac2 > 0, color(ltblue) bin(50)) (hist frac5 if frac5 > 0, color(edkblue) bin(50)) ///
	, legend(order(1 "Frac Base" 2 "Frac +1" 3 "Frac +5") cols(3) size(small)) ///
	graphregion(color(white)) bgcolor(white) ylabel(,nogrid) ylabel(,labsize(vsmall)) xlabel(, labsize(vsmall)) ///
	name(hold, replace) ytitle(, size(small)) xtitle(, size(small))
graph save hold, replace
graph export land`v'ival`b'.png, replace
	
********************************************************************************
* Pairwise versus Share diff hist
********************************************************************************

rename var1 ParcelDiff
rename var2 ShareDiff

*hist ParcelDiff

sum ParcelDiff
local pmean = r(mean)
local reportval1 = string(r(mean),"%6.2f")

twoway (hist ParcelDiff , color(midblue) lcolor(black) bin(100)) ///
	, legend(off) xtick(#6) xlabel(#6 `pmean' "`reportval1'" ) 	///
	graphregion(color(white)) bgcolor(white) ylabel(,nogrid) ylabel(,labsize(vsmall)) xlabel(, labsize(vsmall)) ///
	name(hold, replace) ytitle(, size(small)) xtitle(, size(small))
graph save hold, replace
graph export parceldiff.png, replace


*hist ShareDiff [fweight = var3] if ShareDiff !=0 & ShareDiff > `bd', bin(50)

sum ShareDiff [fweight = var3] if ShareDiff != 0, det
local bd = r(p1)
local pmean = r(mean)
local reportval1 = string(r(mean),"%6.2f")

twoway (hist ShareDiff [fweight = var3] if ShareDiff !=0 & ShareDiff > `bd' , color(midgreen) lcolor(black) bin(100)) ///
	, legend(off) xtick(#6) xlabel(#6 `pmean' "`reportval1'" ) 	///
	graphregion(color(white)) bgcolor(white) ylabel(,nogrid) ylabel(,labsize(vsmall)) xlabel(, labsize(vsmall)) ///
	name(hold, replace) ytitle(, size(small)) xtitle(, size(small))
graph save hold, replace
graph export sharediff.png, replace


********************************************************************************
* Compare model - Parcel util
********************************************************************************

rename var1 parcel1
rename var2 parcel2
rename var3 parcel3

gen plotcount = _n	
gen obs = _n
replace plotcount = . if plotcount > 6
replace obs = . if obs > 6
gen bd = 0.6
	
sum parcel1 if parcel1 > 0, det
local bd1 = r(p95)
local mean1 = r(mean)
local reportval1 = string(r(mean),"%6.2f")
replace plotcount = `mean1' - r(sd) if plotcount in 1
replace plotcount = `mean1' + r(sd) if plotcount in 2
sum parcel2 if parcel2 > 0, det
local bd2 = r(p95)
local mean2 = r(mean)
local reportval2 = string(r(mean),"%6.2f")
replace plotcount = `mean2' - r(sd) if plotcount in 3
replace plotcount = `mean2' + r(sd) if plotcount in 4
sum parcel3 if parcel3 > 0, det
local bd3 = r(p95)
local mean3 = r(mean)
local reportval3 = string(r(mean),"%6.2f")
replace plotcount = `mean3' - r(sd) if plotcount in 5
replace plotcount = `mean3' + r(sd) if plotcount in 6

twoway (area bd plotcount if obs < 3, color(midgreen%30)) (area bd plotcount if obs < 5 & obs > 2, color(eltgreen%30)) (area bd plotcount if obs > 4, color(dkgreen%30)) ///
	(hist parcel1 if parcel1 > 0 & parcel1 < `bd1', color(midgreen) bin(50)) (hist parcel2 if parcel2 > 0 & parcel2 < `bd2', color(eltgreen%80) bin(50)) (hist parcel3 if parcel3 > 0 & parcel3 < `bd3', color(dkgreen%80) bin(50)) ///	
	, legend(order(4 "Firm Base" 5 "Firm: Pre-2009" 6 "Firm: No Bonus") cols(3) size(small))	///
	graphregion(color(white)) bgcolor(white) ylabel(,nogrid) ylabel(,labsize(vsmall)) xlabel(, labsize(vsmall)) ///
	name(hold, replace) ytitle(, size(small)) xtitle(, size(small)) ///
	xline(`mean1') xline(`mean2') xline(`mean3') ///
	xtick(#6) xlabel(#6 `mean1' "`reportval1'" `mean2' "`reportval2'" `mean3' "`reportval3'") 

drop bd obs plotcount

graph export CompareFirmUtil.png, replace
